package com.zl.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.system.comm.util.CheckUtil;
import com.system.comm.util.DateUtils;
import com.system.comm.util.JsonUtils;
import com.system.common.pojo.BisBagProject;
import com.system.common.pojo.BisBagRecord;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisNurseBag;
import com.system.common.pojo.BisNurseProject;
import com.zl.service.ZlBisNurseBagService;
import com.zl.service.ZlBisNurseProjectService;

@Controller
@RequestMapping("bag")
public class wechatBagController {
	@Autowired
	private ZlBisNurseBagService zlBisNurseBagService;
	@Autowired
	private ZlBisNurseProjectService zlBisNurseProjectService;
	@RequestMapping("bagList")
	public String goBag(){
        return "/wechat/bag/bagList";		
	}
	
	@RequestMapping("searchBag")
	public void searchBag(Model model,HttpServletResponse response){
		List<BisNurseBag> bisNurseBags=zlBisNurseBagService.selectByExample(null);
		
		List<BisNurseBag> bisNurseBagList=zlBisNurseBagService.bisNurseBagsService(bisNurseBags);
		String str=JsonUtils.toJson(bisNurseBagList);
		CheckUtil.responseW(response, str);
        	
	}
	
	@RequestMapping("goMyBag")
	public String goMyBag(){
		return "wechat/bag/myBag";
        	
	}
	
	@RequestMapping("listMyBag")
	public void listMyBag(HttpSession session,Model model,HttpServletResponse response){
		BisCustomUser bisCustomUser=CheckUtil.getUserSession(session);
		List<BisBagRecord> bagList=zlBisNurseBagService.queryUserBag(bisCustomUser.getCustomUserId());
		String str=JsonUtils.toJson(bagList);
        CheckUtil.responseW(response, str);	
	}
	
	@RequestMapping("checkRobBag")
	public void checkRobBag(String bagId,HttpServletRequest request,HttpServletResponse response,HttpSession session){
			BisCustomUser bisCustomUser=CheckUtil.getUserSession(session);
			Map<String, Object> map=zlBisNurseBagService.checkRobBag(bagId, bisCustomUser);
			String str=JsonUtils.toJson(map);
			CheckUtil.responseW(response, str);
	}
	
	
	@RequestMapping("showBagDetail")
	public String  showBagDetail(Model model,String bag_id){
		BisNurseBag bag=zlBisNurseBagService.queryById(bag_id);
		List<BisBagProject> bagProjectList=zlBisNurseBagService.queryBagProjectByBagId(bag_id);
		double oldPrice=0;
		double newPrice=0;
		String bagProject="";
		List<BisNurseProject> bisNurseProjects=new ArrayList<>();
        for (BisBagProject bisBagProject : bagProjectList) {
        	BisNurseProject bisNurseProject=zlBisNurseProjectService.selectByPrimaryKey(bisBagProject.getProjectId());
        	String name=bisNurseProject.getProjectName();
        	bagProject+=name;
        	int num=bisBagProject.getProjectNum();
        	oldPrice+=bisNurseProject.getRmbPrice()*num;
        	newPrice+=bisBagProject.getProjectNewPrice()*num;
        	bisNurseProject.setProjectNum(String.valueOf(bisBagProject.getProjectNum()));
        	bisNurseProjects.add(bisNurseProject);
 		}	
        double remainTotalPrice=oldPrice-newPrice;
        model.addAttribute("remainTotalPrice", remainTotalPrice);
        model.addAttribute("bagProject", bagProject);
		model.addAttribute("bag", bag);
		model.addAttribute("bagProjectList", bisNurseProjects);
		return "/wechat/bag/showBagDetail";
	}
	
	@RequestMapping("goRobBag")
	public String goRobBag(Model model,String bagId,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		bagId=bagId.substring(1,bagId.length()-1);
		BisNurseBag bag=zlBisNurseBagService.queryById(bagId);
	     int overdue_time=  bag.getOverdueTime();
	     Date date=new Date();
	     Date overdue_date=DateUtils.getDateAfter(date, overdue_time);
	     SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
	     String str=format.format(overdue_date);
	     model.addAttribute("bag",bag);
	     model.addAttribute("overdue_date", str);
	     return "wechat/bag/buyBag";
	}
	
	@RequestMapping("saveBuyBagOrder")
	public void saveBuyBagOrder(HttpServletRequest request,HttpServletResponse response,HttpSession session){
		BisCustomUser bisCustomUser=CheckUtil.getUserSession(session);
		Map<String, Object> map=zlBisNurseBagService.checkBeauty(request, bisCustomUser);
		String str=JsonUtils.toJson(map);
		CheckUtil.responseW(response, str);
	     
	}



	
}
